﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Webknife.I0;

namespace 文件加密解密
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }
        //选择文件
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog1 = new OpenFileDialog();
            fileDialog1.Filter = "文件类型(*.xml,*.txt,*.config)|*.xml;*.txt;*.config|All files (*.*)|*.*";
            fileDialog1.FilterIndex = 1;
            fileDialog1.RestoreDirectory = true;
            if (fileDialog1.ShowDialog() == DialogResult.OK){
                txt_path.Text = fileDialog1.FileName;
            }else{
                txt_path.Text = "";
            }
        }
        //目标路径
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK){
                txt_path1.Text = dialog.SelectedPath;
            }
        }
        //加密文件
        private void Encrypt_Click(object sender, EventArgs e)
        {
            if (txt_path.Text == "" || txt_path1.Text == ""){
                MessageBox.Show("禁止空路径！","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            string path = txt_path.Text;
            string[] arr = path.Split('\\');
            string filename = arr[arr.Length-1];
            string path1 = txt_path1.Text;
            path1 = path1 +"\\"+ filename;

            Webknife.Common.Encrypt.EncryptFile(path, path1, "123456");
            MessageBox.Show("加密成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        //解密文件
        private void Decrypt_Click(object sender, EventArgs e)
        {
            if (txt_path.Text == "" || txt_path1.Text == ""){
                MessageBox.Show("禁止空路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string path = txt_path.Text;
            string[] arr = path.Split('\\');
            string filename = arr[arr.Length - 1];
            string path1 = txt_path1.Text;
            path1 = path1 + "\\" + filename;
            Webknife.Common.Encrypt.DecryptFile(path, path1, "123456");
            MessageBox.Show("解密成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txt_path.Text == "" || txt_path1.Text == ""){
                MessageBox.Show("禁止空路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string path = txt_path.Text;
            string[] arr = path.Split('\\');
            string filename = arr[arr.Length - 1];
            string path1 = txt_path1.Text;
            path1 = path1 + "\\" + filename;
            string path_str= FileObject.ReadFile(path);

            string enc = Webknife.Common.Encrypt.Encode(path_str, "123456");
            FileObject.WriteFile(path1, enc);
            MessageBox.Show("加密成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (txt_path.Text == "" || txt_path1.Text == "")
            {
                MessageBox.Show("禁止空路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string path = txt_path.Text;
            string[] arr = path.Split('\\');
            string filename = arr[arr.Length - 1];
            string path1 = txt_path1.Text;
            path1 = path1 + "\\" + filename;
            string path_str = FileObject.ReadFile(path);

            string enc = Webknife.Common.Encrypt.Decode(path_str, "123456");
            FileObject.WriteFile(path1, enc);
            MessageBox.Show("解密成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
     
       
    }
}
